<template>
  <div class="edit-name">
    <!-- 导航 -->
    <van-nav-bar
      title="昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onComplete"
    />
    <!-- Field区域 -->
    <div class="field-wrap">
      <van-field
        v-model="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
    <button @click="$emit('update:gender',0)">修改性别</button>
    <button @click="$emit('update:abc',0)">修改abc</button>
    <button @click="$emit('update:a',0)">修改a</button>
  </div>
</template>

<script>
import { updateUserInfo } from "@/api/user";

export default {
  name: "EditName",
  components: {},
  props: {
    // 声明接收父组件v-model传递的value数据
    value: {
      type: String,
    },
    gender: {
      type: Number
    },
    abc: {
      type: String
    },
    a: {
      type: String
    }
  },
  data() {
    return {
      localName: this.value
    };
  },
  computed: {},
  watch: {},
  methods: {
    async onComplete() {
      this.$toast.loading({
        message: "修改中...",
        forbidClick: true
      })
      try {
        await updateUserInfo({
          name: this.localName
        });
        // console.log("修改昵称",data);
        this.$toast.success('修改成功');
        // 发布input事件，更新父组件v-model绑定的属性
        this.$emit("input", this.localName);
        // this.$emit("update-name",this.localName);
        this.$emit('close');

      } catch (err) {
        if (err && err.response && err.response.status === 409) {
          this.$toast.fail("昵称重复")
        }
      }
      
    },
  },
  created() {},
  mounted() {},
};
</script>

<style lang="less" scoped>
//@import url(); 引入公共css类
.edit-name {
  .field-wrap {
    padding: 10px;
  }
}

</style>
